package com.trkj.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.trkj.dao.MemberDAO;
import com.trkj.dao.MemberDAOFactory;
import com.trkj.entity.Member;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private MemberDAO memberDAO = MemberDAOFactory.createDAO();
	
	@Override
	protected void doPost(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		String loginName = request.getParameter("login_name");
		String password = request.getParameter("password");
		System.out.println("xxx");
		Member member = memberDAO.login(loginName, password);
		
		String error = "对不起，登陆失败，请核对后重新登陆！";
		String url = "login.jsp";
		if(member != null){
			//登陆成功
			url = "member_list?curpage=1&pagesize=2";
			//保存到session
			request.getSession().setAttribute("member", member);
			response.sendRedirect(url);
		}else{
			//保存错误信息
			request.setAttribute("error", error);
			request.getRequestDispatcher(url)
				.forward(request, response);
		}
		
	}
}






